/*
 * Copyright (C) 2010 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
 * limitations under the License.
  */
package app.tellwmata;

import android.content.Context;
import android.content.SharedPreferences;

import android.os.Bundle;

import android.preference.ListPreference;
import android.preference.Preference;

import android.preference.Preference.OnPreferenceChangeListener;

import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

public final class Settings extends PreferenceActivity {
    private static final String HOME_TYPE = "home_type";
    private static final CharSequence[] homeScreenValues = new CharSequence[] {
                                                                                  Integer.toString(Constants.TYPE_METRO),
                                                                                  Integer.toString(Constants.TYPE_BUS),
                                                                                  Integer.toString(Constants.TYPE_PARATRANSIT)
    };

    public static int getHomeType(final Context c) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        return Integer.parseInt(prefs.getString(HOME_TYPE, Integer.toString(Constants.TYPE_METRO)));
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPreferenceScreen(createPreferenceHierarchy());
    }

    private PreferenceScreen createPreferenceHierarchy() {
        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

        final CharSequence[] homeScreenEntries = new CharSequence[] {
                                                                        getString(R.string.metro),
                                                                        getString(R.string.buses),
                                                                        getString(R.string.paratransit)
        };

        // 
        ListPreference homeScreenPref = new ListPreference(this);
        homeScreenPref.setEntries(homeScreenEntries);
        homeScreenPref.setEntryValues(homeScreenValues);
        homeScreenPref.setDialogTitle(R.string.home_type);
        homeScreenPref.setKey(HOME_TYPE);
        homeScreenPref.setTitle(R.string.home_type);
        homeScreenPref.setDefaultValue(Integer.toString(Constants.TYPE_METRO));
        homeScreenPref.setSummary(homeScreenPref.getValue());
        Android.updateListPrefSummary((ListPreference) homeScreenPref, Integer.toString(getHomeType(this)));

        homeScreenPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    Android.updateListPrefSummary((ListPreference) preference, newValue.toString());

                    return true;
                }
            });

        root.addPreference(homeScreenPref);

        return root;
    }
}
